<?php

class ShopController extends Controller
{
	public $breadcrumbs;
	public $menu;
	public $_model;
        public $layout;

        public function actionInstall() 
	{
		if($this->module->debug) 
		{
			if(Yii::app()->request->isPostRequest) 
			{
				if($db = Yii::app()->db) {

				} else {
					throw new CException(Yii::t('shop', 'Database Connection is not working'));	
				}
			}
			else {
				$this->render('install');
			}
		} else {
			throw new CException(Shop::t('Webshop is not in Debug Mode'));	
		}
	}

	public function filters()
	{
		return array(
			'accessControl',
		);
	}	

	public function accessRules() {
		return array(
				array('allow',
					'actions'=>array('install', 'index'),
					'users' => array('*'),
					),
				array('allow',
					'actions'=>array('admin'),
					'users' => array('*'),
					),
//				array('deny',  // deny all other users
//						'users'=>array('*'),
//						),
				);
	}


	public function actionIndex()
	{
		$this->redirect(array('//shop/products/index'));
	}
    
    public function actionSearchAuto(){
        $res = array();
        if (isset($_GET['term'])){
            $name = $_GET['term'];
            $criteria = new CDbCriteria;
//            $criteria->compare('name', $name);
            $criteria->condition = "name LIKE :name";
            $criteria->params = array(":name"=>"%$name%");
            $packages = Package::model()->findAll($criteria);
            $products = Product::model()->findAll($criteria);
            foreach ($packages as $package){
                $res[] = $package->name;
            }     
            
            foreach ($products as $product)
                $res[] = $product->name;
        }
        echo CJSON::encode($res);
        Yii::app()->end();
    }
    public function actionSearch(){
        $packages = array();
        if (isset($_GET['case'])){
            $name = $_GET['case'];
            $criteria = new CDbCriteria;
//            $criteria->compare('name', $name);
            $criteria->condition = "name LIKE :name ";
            $criteria->params = array(":name"=>"%$name%");
            $packages = Package::model()->findAll($criteria);
            
            //search  product
            $criteria->condition = "name LIKE :name OR description LIKE :name";
            $criteria->params = array(":name"=>"%$name%");
            $products = Product::model()->findAll($criteria);
        }
        $this->render('result', array(
            'packages' => $packages,
            'products' => $products
        ));
    }
    
    public function actionResult(){
        $result = array();
        $limit  = 3;
        if (isset($_POST['id']) && isset($_POST['type'])){
            $id = $_POST['id'];
            $type = $_POST['type'];
            $packages = Package::model()->findByPk($id);
            $products = $packages->readPackage();

            foreach ($products as $product){
                $result[] = $product;
            } 
        }
        echo json_encode($result);
    }
    
    public function actionDistributor(){
        $name = $_POST['name'];
        $amount = $_POST['amount'];
        $products = array();
        if (isset($name)){
            $criteria = new CDbCriteria;
//            $criteria->compare('name', $name);
            $criteria->condition = "name LIKE :name";
            $criteria->params = array(":name"=>"%$name%");
            $products = Product::model()->findAll($criteria);
        }
        if (!isset($amount))
            $amount = 1;
        $this->renderPartial('distributor', array('products' => $products, 'amount' => $amount));
    }
    public function actionListDistributor($id){

        $distributors = new CActiveDataProvider('DistributorProduct', array(
                    'criteria' => array(
                        'condition' => 'product_id = ' .$id. ' ORDER BY unit_price'
                    )
                ));
        $this->render('agent', array(
           // 'packages' => $packages,
            'distributors' => $distributors
        ));
    }


    public function actionPackage($id){
        
        $xmlPath = Yii::getPathOfAlias('webroot').Package::XML_PACKAGE_PATH;
        $package = Package::model()->findByPk($id);
        $config = simplexml_load_file($xmlPath.$package->file.'.xml');
       // var_dump($config);
        
        
        //$config = new SimpleXMLElement;        
        
        //var_dump($config->attributes());
        //echo $config->getName().'<br />';
        //$config->
        //$config = new DOMDocument;
        //$config->
        //$config->load($xmlPath.$package->file.'.xml');
        //var_dump($config->getElementsByTagName('package'));
       // $cur_package = new SimpleXMLElement($package_xml);
        //echo $package_xml->asXML();
        $this->render('package',array(
            'data'=>$package,
            'config'=>$config
        ));
        
    }
    
    public function actionService($id){
        
        $xmlPath = Yii::getPathOfAlias('webroot').Package::XML_PACKAGE_PATH;
        $package = Package::model()->findByPk($id);
        $products = $package->readPackage();
        $this->render('service',array(
            'products'=>$products,
            'data'  => $package
            
        ));
        
    }
    
    public function actionAddToCart(){
        if (isset($_POST)){
		$cart = Shop::getCartContent();

		// remove potential clutter
		if(isset($_POST['yt0']))
			unset($_POST['yt0']);
		if(isset($_POST['yt1']))
			unset($_POST['yt1']);
                if (!isset($_POST['ShippingMethod']))
                    $_POST['ShippingMethod'] = '';

		$cart[] = $_POST;
	
		Shop::setCartcontent($cart);
        echo 1;
        }else{
            echo 0;
        }
		
    }
}
